﻿Imports PizzaManiaDAL 'Type sharing for business rules reuse

Class Window1
    Private proxy As New PizzaService.Service1Client
    Private Customer As Customer

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        If Me.txtSearch.Text <> "" Then
            Me.Customer = proxy.GetCustomerByPhoneNumber(Me.txtSearch.Text)

            If Me.Customer Is Nothing Then
                If MessageBox.Show("Customer not found. Would you like to create a new customer record?", Me.Title, MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
                    Me.Customer = New Customer()
                    Me.Customer.Name = ""
                End If
            End If
        End If
        If Me.Customer IsNot Nothing Then
            Me.DataContext = Me.Customer
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Me.Validate()

        If Me.Customer.IsDirty Then
            If Me.Customer.HasErrors Then
                MsgBox(Me.Customer.Error)
            Else
                Try
                    If proxy.SaveCustomer(Me.Customer, False) Then
                        MsgBox("Saved!")
                    Else
                        If Me.Customer.HasErrors Then
                            MsgBox(Me.Customer.Error)
                        End If
                    End If

                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub Validate()
        Try
            Me.Customer.Validate()
        Catch ex As ValidationException
            'CollectionViewSource.GetDefaultView(Me.Customer).Refresh()
        End Try

    End Sub
End Class
